package com.shundr.auth.common.util;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

/**
 * 
 * ProgressDialogHelper
 * 
 * @author larry 2015-4-17 下午2:42:16
 * @version 1.0.0
 * 
 */
public class ProgressDialogHelper {

	private static ProgressDialog mProgressDialog;

	/**
	 * 显示progressdialog
	 * 
	 * @param context
	 *            上下文对象
	 * @param msg
	 *            显示的内容
	 */
	public static void show(Context context, String msg) {
		mProgressDialog = new ProgressDialog(context);
		// 设置默认样式
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		mProgressDialog.setCanceledOnTouchOutside(false);
		mProgressDialog.setMessage(msg);
		if (!mProgressDialog.isShowing()) {
			mProgressDialog.show();
		}
	}

	public static void setMessageText(String msg) {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.setMessage(msg);
		}
	}

	/**
	 * 隐藏progressdialog
	 */
	public static void dismiss() {
		if (mProgressDialog != null) {
			// if (mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
			mProgressDialog = null;
			// }
		}
	}

	/**
	 * 初始化等待进度条
	 * 
	 * @param context
	 */
	public static void init(Context context) {
		if (mProgressDialog == null) {
			// 初始化ProgressDialog控件
			mProgressDialog = new ProgressDialog(context);
			// 设置默认样式
			mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		}
	}

	public static void showSetTimeOut(final Context mContext, String msg,
			final String dismissMsg, int seconds) {
		mProgressDialog = new ProgressDialog(mContext);
		// 设置默认样式
		mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		// }
		mProgressDialog.setMessage(msg);
		mProgressDialog.setCancelable(true);
		mProgressDialog.show();
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				((Activity) mContext).runOnUiThread(new Thread() {
					@Override
					public void run() {
						mProgressDialog.setMessage(dismissMsg);
					}
				});
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				mProgressDialog.dismiss();
			}
		}, seconds * 1000);

	}
}
